<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/8/25
 * Time: 11:31
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Consignorder extends Base
{
    /**
     * 寄存订单列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $order_number = input('order_number'); // 订单编号
            $user_username = input('user_username'); // 用户
            $user_phone = input('user_phone'); // 手机号
            $order_status = input('order_status'); // 订单状态
        
            $where['order_type'] = 2;
            if(isset($order_number) && $order_number != ''){
                $where['order_number'] = ['like', '%' . $order_number . '%'];
            }
            if(isset($user_username) && $user_username != ''){
                $where['user_username'] = ['like', '%' . $user_username . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($order_status) && $order_status != ''){
                $where['order_status'] = $order_status;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('order')
                ->join('up_u_user', 'user_id = order_user_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('order')
                ->join('up_u_user', 'user_id = order_user_id', 'LEFT')
                ->field('order_id, order_number, user_username, order_status, order_old_price, order_time,  
                order_check_time, order_payfor_time, user_phone, order_is_consign')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('order_is_consign desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 订单详情
     * @return mixed
     */
    public function orderDetail(){
        $order_id = input('order_id');
        $order = Db::table('up_order')
            ->join('up_shop', 'shop_id = order_shop_id', 'LEFT')
            ->join('up_cate', 'cate_id = order_cate_id', 'LEFT')
            ->join('up_brand', 'brand_id = order_brand_id', 'LEFT')
            ->join('up_models', 'models_id = order_models_id', 'LEFT')
            ->join('up_u_user', 'order_user_id = user_id', 'LEFT')
            ->where('order_id', $order_id)
            ->field('cate_name, brand_name, models_name, user_username, user_phone, order_number, order_status, 
            order_old_price, order_time, order_check_time, order_payfor_time, shop_name, order_ri_ids, 
            order_remark')
            ->find();
        $ri_names = Db::table('up_recycle_info')
            ->where('ri_id', 'IN', $order['order_ri_ids'])
            ->column('ri_name');
        $order['ri_names'] = implode(',', $ri_names);
        return $this->fetch('consignorder/orderdetail', ['order' => $order]);
    }
    
    /**
     * 审核通过
     * @return \think\response\Json
     */
    public function orderAgree(){
        $order_id = input('order_id');
        if(Request::instance()->isPost()){
            $price = input('price');
            $order = Db::table('up_order')
                ->join('up_cate', 'cate_id = order_cate_id', 'LEFT')
                ->join('up_brand', 'brand_id = order_brand_id', 'LEFT')
                ->join('up_models', 'models_id = order_models_id', 'LEFT')
                ->where('order_id', $order_id)
                ->field('order_user_id, cate_name, brand_name, models_name')
                ->find();
            $order_info = Db::table('up_order_info')
                ->where('oi_order_id', $order_id)
                ->where('oi_type', 1)
                ->field('oi_id')
                ->find();
    
            Db::startTrans();
            try{
                // 更改订单状态
                Db::table('up_order')->where('order_id', $order_id)->update([
                    'order_status' => 2,
                    'order_check_time' => date('Y-m-d H:i:s'),
                    'order_old_price' => $price,
                    'order_jc_now_price' => $price
                ]);
                // 消息表
                $notice_content = '您申请的' . $order['brand_name'] . $order['models_name'] . $order['cate_name'] . '的物品寄存订单审核已通过，门店将在48小时内放款，请耐心等待';
                Db::table('up_notice')->insert([
                    'notice_title' => '审核已通过',
                    'notice_content' => $notice_content,
                    'notice_type' => 2,
                    'notice_range' => 2,
                    'notice_user' => ',' . $order['order_user_id'] . ',',
                    'notice_read_user' => ',0,',
                    'notice_del_user' => ',0,',
                    'notice_time' => date('Y-m-d H"i:s')
                ]);
                // 续存订单状态
                Db::table('up_order_info')->where('oi_id', $order_info['oi_id'])
                    ->update([
                        'oi_status' => 2 ,
                        'oi_pay' => 2
                    ]);
        
                Db::commit();
                return json(['code' => 200, 'msg' => '操作成功']);
            }catch(\Exception $e){
                Db::rollback();
                return json(['code' => 100, 'msg' => $e->getMessage()]);
            }
        }
        return $this->fetch('consignorder/orderagree', ['order_id' => $order_id]);
    }
    
    /**
     * 审核拒绝
     * @return \think\response\Json
     */
    public function orderRefuce(){
        $order_id = input('id');
        $order = Db::table('up_order')
            ->join('up_cate', 'cate_id = order_cate_id', 'LEFT')
            ->join('up_brand', 'brand_id = order_brand_id', 'LEFT')
            ->join('up_models', 'models_id = order_models_id', 'LEFT')
            ->where('order_id', $order_id)
            ->field('order_user_id, cate_name, brand_name, models_name')
            ->find();
        $order_info = Db::table('up_order_info')
            ->where('oi_order_id', $order_id)
            ->where('oi_type', 1)
            ->field('oi_id')
            ->find();
    
        Db::startTrans();
        try{
            // 更改订单状态
            Db::table('up_order')->where('order_id', $order_id)->update([
                'order_status' => 4,
                'order_check_time' => date('Y-m-d H:i:s')
            ]);
            // 消息表
            $notice_content = '您申请的' . $order['brand_name'] . $order['models_name'] . $order['cate_name'] . '的物品回收订单未通过审核';
            Db::table('up_notice')->insert([
                'notice_title' => '审核未通过',
                'notice_content' => $notice_content,
                'notice_type' => 2,
                'notice_range' => 2,
                'notice_user' => ',' . $order['order_user_id'] . ',',
                'notice_read_user' => ',0,',
                'notice_del_user' => ',0,',
                'notice_time' => date('Y-m-d H"i:s')
            ]);
            // 续存订单状态
            Db::table('up_order_info')->where('oi_id', $order_info['oi_id'])->setField('oi_status', 3);
        
            Db::commit();
            return json(['code' => 200, 'msg' => '操作成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 打款
     * @return \think\response\Json
     */
    public function orderPay(){
        $order_id = input('id');
    
        $res = Db::table('up_order')->where('order_id', $order_id)->update([
            'order_status' => 3,
            'order_payfor_time' => date('Y-m-d H:i:s')
        ]);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 续存订单列表
     * @return mixed|\think\response\Json
     */
    public function orderXC(){
        if(request()->isAjax ()){
            $where['oi_order_id'] = session('order_id');
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('order_info')->where($where)->count();// 获取总条数
            $lists = Db::name('order_info')
                ->where($where)
                ->field('oi_id, oi_days, oi_price, oi_date, oi_zhejiu_price, oi_type, oi_status, oi_time, oi_cate, oi_order_number')
                ->page($Nowpage,$limits)
                ->order('oi_time desc')
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $order_id = input('order_id');
        if(isset($order_id) && $order_id != ''){
            session('order_id', $order_id);
        }
        return $this->fetch('consignorder/orderxc');
    }
    
    /**
     * 审核通过
     * @return mixed|\think\response\Json
     */
    public function orderInfoZheJ(){
        $oi_id = input('oi_id');
        if(Request::instance()->isPost()){
            $zhejiu = input('zhejiu');
            $oi = Db::table('up_order_info')->where('oi_id', $oi_id)->field('oi_order_id')->find();
            Db::startTrans();
            try{
                // 修改续存表
                Db::table('up_order_info')->where('oi_id', $oi_id)->update([
                    'oi_status' => 2,
                    'oi_zhejiu_price' => $zhejiu
                ]);
                // 修改订单主表
                Db::table('up_order')->where('order_id', $oi['oi_order_id'])->update([
                    'order_is_consign' => 2,
                    'order_jc_zhejiu_price' => $zhejiu
                ]);
                
                Db::commit();
                return json(['code' => 200, 'msg' => '操作成功']);
            }catch(\Exception $e){
                Db::rollback();
                return json(['code' => 100, 'msg' => $e->getMessage()]);
            }
        }
        return $this->fetch('consignorder/orderinfozhej', ['oi_id' => $oi_id]);
    }
    
    /**
     * 拒绝续存
     * @return \think\response\Json
     */
    public function orderInfoRefuce(){
        $oi_id = input('id');
        $oi = Db::table('up_order_info')->where('oi_id', $oi_id)->field('oi_order_id')->find();
        Db::startTrans();
        try{
            // 修改续存表
            Db::table('up_order_info')->where('oi_id', $oi_id)->update([
                'oi_status' => 3
            ]);
            // 修改订单主表
            Db::table('up_order')->where('order_id', $oi['oi_order_id'])->update([
                'order_is_consign' => 2
            ]);
            
            Db::commit();
            return json(['code' => 200, 'msg' => '操作成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => $e->getMessage()]);
        }
    }
}